/// <reference path="../../inig.d.ts" />

import { GlobalData } from './global'

export default function getCookieByName(name: string) {
  if (!name) {
    return ''
  }
  let inigIsServer = GlobalData.isServer
  let inigIsTest = GlobalData.isTest
  if (!inigIsServer && !inigIsTest) {
    if (!(' ' + document.cookie).match(new RegExp(' ' + name + '='))) {
      return ''
    }
    return document.cookie.replace(
      new RegExp('(.{0,} ?' + name + '=)([^;]*)(;?.*)'),
      '$2'
    )
  } else if (inigIsServer) {
    /**
     * 服务端渲染
     */
    let inigCookies = GlobalData.cookies
    return inigCookies[name] || ''
  } else {
    return ''
  }
}
